package cn.kgc.crowdfundingadminspringboot.service.impl;

import cn.kgc.crowdfundingadminspringboot.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadminspringboot.mapper.CertMapper;
import cn.kgc.crowdfundingadminspringboot.pojo.*;
import cn.kgc.crowdfundingadminspringboot.service.CertService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class CertServiceImpl implements CertService {
    @Resource
    CertMapper certMapper;
    @Override
    public PageInfo<Cert> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Cert> pageInfo=null;
        if (CrowdFundingUtils.stringEffective(keyword)){
            CertExample example=new CertExample();
            CertExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+keyword+"%");
            List<Cert> list=certMapper.selectByExample(example);
            pageInfo=new PageInfo<>(list);
        }else{
            List<Cert> list=certMapper.selectByExample(null);
            pageInfo=new PageInfo<>(list);
        }
        return pageInfo;
    }

    @Override
    public List<Cert> getCertListByIdList(List<Integer> certIdList) {
        // 创建实体类Role对应的Example对象
        CertExample certExample = new CertExample();
        // 在Example对象中封装查询条件
        certExample.createCriteria().andIdIn(certIdList);
        // 执行查询
        return certMapper.selectByExample(certExample);
    }


    @Override
    public void batchRemove(List<Integer> certIdList) {

        CertExample certExample = new CertExample();

        certExample.createCriteria().andIdIn(certIdList);

        certMapper.deleteByExample(certExample);
    }
    @Override
    public void saveCert(String name) {
        certMapper.insert(new Cert(null, name));
    }


    @Override
    public void updateCert(Cert cert) {
        certMapper.updateByPrimaryKey(cert);
    }
}
